
import cv2
from PIL import Image, ImageTk  
from 城市判断 import city_logic_map

black=(0,0,0,)
red=(0,0,255)
green=(0,255,0)
blue=(255,0,0)
white=(255,255,255)
yellow=(0,255,255)


cities_info = {
   
    #地图名，地图编号，右上角最大x,y, 地图的W和H
    "江南野外": (0,(159,119),(370,276),),   
    "大唐境外": (1,(638,118),(583,108),),   
    "建邺城":   (2,(287,143),(556,276),),     
    "朱紫国":   (3,(191,119),(441,277),),   
    "傲来国":   (4,(223,150),(409,279),),    
    "宝象国":   (5,(159,119),(442,333),),  
    "西凉女国": (6,(163,123),(373,284),),  
    "女儿村":   (7,(127,143),(322,361),),         
    "长寿村":   (8,(159,209),(267,349),),     
    "普陀山":   (9,(95, 71),(371,279),),     
    "五庄观":   (10,(99,74),(370,277),),      
    }

    
def 获取地图信息(city):
    city_info = cities_info[city]
    return city_info

def dingwei(entry61,radio_value,label90):
    entry_text=entry61.get()
    x,y=entry_text.split(".")
    #print(radio_value.get(),x,y)
    entry61.delete(0,"end")
    city_info = cities_info[radio_value.get()]
    show_image(label90,city_info,x,y)

    

def show_image(label90,city_info,x,y):
    
    city_num,coords,img_info=city_info
    city_image=根据地图信息画图(city_info,x,y)
    
    rgb_img = cv2.cvtColor(city_image, cv2.COLOR_BGR2RGB)
    
    # 2. 转换为 PIL.Image 格式
    image= Image.fromarray(rgb_img)
    
    label90_width=416
    label90_height=300
    
    scale1 = label90_width / img_info[0]
    scale2 = label90_height / img_info[1]
    scale=min(scale1,scale2)
    new_size = (int(img_info[0] * scale), int(img_info[1] * scale))
    image = image.resize(new_size)  # 缩放图像（可选）
    photo = ImageTk.PhotoImage(image)
    
    # 计算 padx 和 pady 来居中图片
    padx = (label90_width - image.width) // 2
    pady = (label90_height - image.height) // 2
        
    label90.grid(padx=padx,pady=pady)
    label90.config(image=photo)
    label90.image = photo  # 保持引用，避免图片被垃圾回收

def 根据地图信息画图(city_info,x,y):
    
    x,y=int(x),int(y)  #地图坐标
    city_num,coords,img_info=city_info
    city_img="./map/"+str(city_num)+".jpg"
    city_image = cv2.imread(city_img)
    #image = Image.open(city_img)  # 替换为你的图片路径
    
    # 计算缩放因子
    scale_x = img_info[0] / coords[0]    #X轴1个坐标=？像素
    scale_y = img_info[1] /coords[1]     #X轴1个坐标=？像素
    
    #对应图像上坐标
    x_pixel = int(scale_x * x)
    y_pixel = img_info[1]- int(scale_y * y)

    #地图范围
    radius=50
    #对应图像上的范围
    radius_x=int(scale_x *50)+1
    radius_y=int(scale_y *50)+1

    # 计算正方形的四个角
    top_left = (x_pixel- radius_x,  y_pixel- radius_y)
    bottom_right = (x_pixel+ radius_x,  y_pixel + radius_y)
    
    # 画正方形
    cv2.rectangle(city_image, top_left, bottom_right, red, 2)
    # 画十字架
    cv2.line(city_image, (x_pixel- radius_x,  y_pixel), (x_pixel+ radius_x,  y_pixel), red, 2)
    cv2.line(city_image, (x_pixel,  y_pixel-radius_y), (x_pixel,  y_pixel+ radius_y), red, 2)
    
    #定位范围
    overlay = city_image.copy()
    alpha=0.5  #設置透明度，将叠加层与原始图片进行混合
    
    image=图形定位(city_num,overlay,x,y,x_pixel,y_pixel,radius_x,radius_y)
    result_image = cv2.addWeighted(image, alpha, city_image, 1 - alpha, 0)
    
    #画坐标位置,坐标在最上面显示，最后画
    cv2.circle(result_image, (x_pixel,y_pixel),5, blue, -1)   # 畫紅色圓圈
    return result_image

def draw_image(overlay, points, quadrants, color):
    for q in quadrants:
        p1, p2 = points[q]
        cv2.rectangle(overlay, p1, p2, color, -1)
    return overlay

def 图形定位(city_num,overlay,x,y,x_pixel,y_pixel,radius_x,radius_y):
    
    # 定义4个象限对应的矩形区域
    points = {
        1: ((x_pixel - radius_x, y_pixel - radius_y), (x_pixel, y_pixel)),            # 左上
        2: ((x_pixel, y_pixel - radius_y), (x_pixel + radius_x, y_pixel)),            # 右上
        3: ((x_pixel - radius_x, y_pixel), (x_pixel, y_pixel + radius_y)),            # 左下
        4: ((x_pixel, y_pixel), (x_pixel + radius_x, y_pixel + radius_y))             # 右下
    }

    handler = city_logic_map[city_num]
    selected_quadrants = handler(x, y)
                   
               
    # 调用外部子程序绘图
    result_image = draw_image(overlay, points, selected_quadrants, green)           

    return result_image
    
    